<template>
	<el-dialog :close-on-click-modal="false" :title="titlethis" :visible.sync="show" v-if="show" width="80%" v-loading="loading">

		<div style="padding:0px 0px 20px 0px;">
			<el-form ref="form" :model="request.data" :inline="true" label-width="80px" :rules="rules">
				<el-row>
					<input type="checkbox" @click="checkAll()" v-model="isAll"> 全选{{chsd.length}}单,合计{{selectmon}}元
				</el-row>
				<br>
				<el-card>
					<el-tag :key="tag" v-for="tag in tclist" style="margin: 0 5px;">
						{{tag}}
					</el-tag>
				</el-card>
			</el-form>
		</div>
		<div class="dataTable" v-if="listData" width="100%">
			<el-table :data="listData" stripe border v-loading="listLoading">
				<el-table-column type="index" label="序号" width="55" fixed="left"></el-table-column>
				<el-table-column label="选中" width="55" fixed="left">
					<template slot-scope="scope">
						<input type="checkbox" :value="scope.$index" v-model="chsd">
					</template>
				</el-table-column>
				<el-table-column prop="rname" label="姓名"></el-table-column>
				<el-table-column prop="mnum" label="工号"></el-table-column>
				<el-table-column prop="phone" label="电话"></el-table-column>
				<el-table-column prop="eaddrs" label="子地址"></el-table-column>

				<el-table-column prop="selectTc" label="套餐名称"  v-if="parameter.etype==2">
					<template slot-scope="scope">
						<div v-for="item,index in scope.row.selectTc">
							<div v-if="item.oid==scope.row.oid">
								<span>{{index+1}}.{{item.title}}
								</span>
							</div>
						</div>
					</template>
				</el-table-column>
				<el-table-column v-else prop="namels" label="套餐名称"></el-table-column>

				<el-table-column prop="jdtm" label="下单时间"></el-table-column>
				<el-table-column prop="xdtm" label="接单时间"></el-table-column>
				<el-table-column prop="cost" label="定价">
				</el-table-column>
				<el-table-column prop="state" label="状态">

					<template slot-scope="scope" v-if="scope.row.state">
						<div v-if="scope.row.state==1">
							<el-tag type="warning">待接单</el-tag>
						</div>
						<div v-if="scope.row.state==2">
							<el-tag type="success">已接单</el-tag>
						</div>
						<div v-if="scope.row.state==3">
							<el-tag type="success">已配送</el-tag>
						</div>
						<div v-if="scope.row.state==4">
							<el-tag type="danger">已拒单</el-tag>
						</div>
					</template>

				</el-table-column>

				<el-table-column label="操作" fixed="right" width="150px">
					<template slot-scope="scope">
						<div class="dataTable-operator">
							<el-button type="info" @click="ckFoods(scope.$index, scope.row)">查看菜品</el-button>
						</div>
					</template>
				</el-table-column>
			</el-table>

		</div>
		<div slot="footer" class="dialog-footer" v-if="parameter.state!=3">
			<el-button type="primary" @click="submitForm('form')">确 定</el-button>
			<el-button @click="show = false">取 消</el-button>
		</div>
		<div slot="footer" class="dialog-footer" v-else>
			<center>
				<el-button @click="show = false">关 闭</el-button>
			</center>
		</div>
		<editDialog :parameter="editParameter" :isDisplay.sync="editDisplay"></editDialog>

	</el-dialog>

</template>

<script>
	import utils from 'util/utils.js';

	import constant from 'util/constant.js';
	import validate from 'util/validate.js';
	import api from 'api/order/currentorder/currentorder.js';
	import editDialog from './caipin.vue';
	export default {
		name: 'edit',
		components: {
			editDialog
		},
		data() {
			return {
				selectmon: 0,
				isAll: false,
				chsd: [], //选中的
				sendData: [],
				tclist: [],

				constant: constant,
				listData: [],
				titlethis: '',
				editDisplay: false,
				editParameter: {},
				loading: false,
				show: this.isDisplay,
				request: utils.newRequest(),
				requestCount: utils.newRequest(),
				rules: {
					mid: [{
						validator: validate.required,
						trigger: 'blur'
					}]
				}
			};
		},
		methods: {
			checkAll() {
				this.chsd = []
				this.sendData = []
				if (!this.isAll) {
					for (let index = 0; index < this.listData.length; index++) {
						this.chsd.push(index)
					}
				}
			},
			//查看菜品
			ckFoods(index, row) {
				if (this.parameter.etype == 2) {
					this.editDisplay = true;
					console.log(row.selectTc);
					this.editParameter = {
						eaddrs: row.mnum,
						selectTc: row.selectTc,
						sendact: row.sendact,
						isMore: true
					};
				} else {
					this.editDisplay = true;
					this.editParameter = {
						eaddrs: row.mnum,
						fnameslist: row.fnames,
						sendact: row.sendact,
						isMore: false
					};
				}

			},
			//获取数据
			list() {
				this.request = utils.newRequest();
				this.listLoading = true;
				this.request.param.ename = this.parameter.eaddrs
				this.request.param.otype = this.parameter.type
				console.log(this.parameter.state + "--------------eid")
				if (this.parameter.state == 1) {
					if (this.parameter.etype == 2) {
						this.request.param.etype = this.parameter.etype;
						this.request.param.ostate = this.parameter.state;
						api.selectJdPrint(this.request, response => {
							this.listData = response.data;
						});
					} else {
						this.request.param.ostate = this.parameter.state;
						api.selectJdPrintOne(this.request, response => {
							console.log(response.data)
							this.listData = response.data;
						});
					}

				}
				if (this.parameter.state == 2) {

					if (this.parameter.etype == 2) {
						this.request.param.etype = this.parameter.etype;
						this.request.param.ostate = this.parameter.state;
						api.selectJdPrint(this.request, response => {
							this.listData = response.data;
						});
					} else {
						this.request.param.ostate = this.parameter.state;
						api.selectJdPrintOne(this.request, response => {
							this.listData = response.data;
						});
					}

				}
				if (this.parameter.state == 3) {
					if (this.parameter.etype == 2) {
						this.request.param.etype = this.parameter.etype;
						this.request.param.ostate = this.parameter.state;
						api.selectJdPrint(this.request, response => {
							this.listData = response.data;
						});
					} else {
						this.request.param.ostate = this.parameter.state;
						api.selectJdPrintOne(this.request, response => {
							this.listData = response.data;
						});
					}
				}

				// js 获取当前时间 年月日
				var datetime = new Date();
				var year = datetime.getFullYear();
				var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
				var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
				var dates = year + "-" + month + "-" + date;



				//请求合计菜品
				this.requestCount.param.jdtm = dates;
				this.requestCount.param.eid = this.parameter.eid;
				this.requestCount.param.state = this.parameter.state;
				api.countTc(this.requestCount, response => {
					const atrr = []
					console.log(response)

					for (let index = 0; index < response.data.length; index++) {
						if (response.data[index].mdh != undefined) {

							var mdh = response.data[index].title + '' + response.data[index].mdh + '份'
							atrr.push(mdh);
						}
						if (response.data[index].fdh != undefined) {
							var fdh = response.data[index].title + '' + response.data[index].fdh + '份'
							atrr.push(fdh);
						}

						if (response.data[index].mfd != undefined) {
							var mfd = response.data[index].title + '' + response.data[index].mfd + '份'
							atrr.push(mfd);
						}
						if (response.data[index].otrs != undefined) {
							var otrs = response.data[index].title + '' + response.data[index].otrs + '份'
							atrr.push(otrs);
						}
						if (response.data[index].vgtb != undefined) {
							var vgtb = response.data[index].title + '' + response.data[index].vgtb + '份'
							atrr.push(vgtb);
						}

					}
					this.tclist = atrr;
				});
				this.listLoading = false;
			},
			//提交表单
			submitForm(formName) {
				this.sendData = []
				let then = this
				utils.checkForm(this.$refs[formName], () => {
					//chsd里面放的都是行号，只需要根据行号获取id、cost，即可。
					if (then.chsd.length != 0) {
						then.chsd.forEach(item => {
							for (var i = 0; i < then.listData.length; i++) {
								then.sendData.push(then.listData[item].oid);
							}
						})
					} else {
						utils.msg.warning("请选择打印订单");
					}

					console.log(then.sendData)
					if (then.sendData.length != 0) {
						this.request.param.ids = then.sendData;

						//放id集合。string内容
						then.request.param.ids = then.sendData;
						//发送修改状态代码
						this.loading = true;
						api.update(
							then.request,
							response => {
								utils.msg.success(response.msg);
								then.loading = false;
								then.show = false;
								then.$parent.list(then.$parent.request);
							},
							response => {
								utils.msg.warning(response.msg);
								then.loading = false;
							}
						);
					}
				});
			},
			init() {
				if (this.parameter.state == 1) {
					this.titlethis = this.parameter.eaddrs + ' - 接单打印';
				} else if (this.parameter.state == 2) {
					this.titlethis = this.parameter.eaddrs + ' - 重新打单';
				} else {
					this.titlethis = this.parameter.eaddrs + ' - 查看订单';
				}
				this.list();
				this.checkAll();

			}
		},
		props: {
			isDisplay: {
				type: Boolean,
				default () {
					return false;
				}
			},
			parameter: {
				type: Array | Object,
				default () {
					return {};
				}
			}
		},
		watch: {
			isDisplay(val) {
				this.show = val;
				if (val) {
					this.init();
				}
			},
			show(val) {
				this.selectmon = 0;
				this.isAll = false;
				this.chsd = [];
				this.sendData = [];
				this.$emit('update:isDisplay', val);

			},
			parameter(val) {
				this.parameter = val;
			},
			chsd: function() {
				let then = this;
				then.selectmon = 0;
				if (then.chsd.length != 0) {
					then.chsd.forEach(item => {
						for (var i = 0; i < then.listData.length; i++) {
							if (item == i) {
								then.selectmon += parseInt(then.listData[item].cost);
								break;
							}
						}
					});
				}
			}

		}
	};
</script>

<style lang="scss">
	@import '~common/custom/css/common.scss';
</style>
